<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import router from '@/router'

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  email: ''
})

const resetForm = reactive({
  email: '',
  verificationCode: '',
  newPassword: '',
  confirmPassword: ''
})

// 当前激活的表单
const activeForm = ref('login') // login, register, reset

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}

// 切换表单
const switchForm = (form) => {
  activeForm.value = form
}

// 登录处理
const handleLogin = () => {
  // TODO: 实现登录逻辑
  ElMessage.success('登录成功')
  router.push('/manage/home')
}

// 注册处理
const handleRegister = () => {
  // TODO: 实现注册逻辑
  ElMessage.success('注册成功')
  switchForm('login')
}

// 重置密码处理
const handleReset = () => {
  // TODO: 实现重置密码逻辑
  ElMessage.success('密码重置成功')
  switchForm('login')
}
</script>
<template>
  <div class="login-container">
    <div class="background-animation"></div>
    <div class="form-container">
      <!-- 登录表单 -->
      <div v-show="activeForm === 'login'" class="form-box">
        <h2>管理员登录</h2>
        <el-form :model="loginForm" :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="用户名">
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="密码">
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <div class="form-actions">
            <el-button type="primary" @click="handleLogin" class="submit-btn">登录</el-button>
          </div>
          <div class="form-links">
            <span @click="switchForm('register')">注册账号</span>
            <span @click="switchForm('reset')">忘记密码？</span>
          </div>
        </el-form>
      </div>

      <!-- 注册表单 -->
      <div v-show="activeForm === 'register'" class="form-box">
        <h2>注册账号</h2>
        <el-form :model="registerForm" :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="registerForm.username" placeholder="用户名">
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="registerForm.password" type="password" placeholder="密码">
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input v-model="registerForm.confirmPassword" type="password" placeholder="确认密码">
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="email">
            <el-input v-model="registerForm.email" placeholder="邮箱">
              <template #prefix>
                <el-icon><Message /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <div class="form-actions">
            <el-button type="primary" @click="handleRegister" class="submit-btn">注册</el-button>
          </div>
          <div class="form-links">
            <span @click="switchForm('login')">返回登录</span>
          </div>
        </el-form>
      </div>

      <!-- 重置密码表单 -->
      <div v-show="activeForm === 'reset'" class="form-box">
        <h2>重置密码</h2>
        <el-form :model="resetForm" :rules="rules">
          <el-form-item prop="email">
            <el-input v-model="resetForm.email" placeholder="邮箱">
              <template #prefix>
                <el-icon><Message /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <div class="verification-code">
              <el-input v-model="resetForm.verificationCode" placeholder="验证码">
                <template #prefix>
                  <el-icon><Key /></el-icon>
                </template>
              </el-input>
              <el-button>获取验证码</el-button>
            </div>
          </el-form-item>
          <el-form-item prop="newPassword">
            <el-input v-model="resetForm.newPassword" type="password" placeholder="新密码">
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input v-model="resetForm.confirmPassword" type="password" placeholder="确认新密码">
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <div class="form-actions">
            <el-button type="primary" @click="handleReset" class="submit-btn">重置密码</el-button>
          </div>
          <div class="form-links">
            <span @click="switchForm('login')">返回登录</span>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<style scoped>
.login-container {
  min-height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  background: linear-gradient(
    135deg,
    #2b4c80 0%,
    #3a6073 50%,
    #3a7bd5 100%
  );
}

.background-animation {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: 
    radial-gradient(circle at 15% 50%, rgba(255, 255, 255, 0.05) 0%, transparent 50%),
    radial-gradient(circle at 85% 30%, rgba(255, 255, 255, 0.05) 0%, transparent 50%),
    radial-gradient(circle at 50% 80%, rgba(255, 255, 255, 0.05) 0%, transparent 50%);
  animation: backgroundMove 20s ease-in-out infinite;
  z-index: 0;
}

@keyframes backgroundMove {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}

.form-container {
  position: relative;
  z-index: 1;
  width: 420px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.form-box {
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

h2 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 30px;
  font-weight: 600;
}

.form-actions {
  margin-top: 24px;
}

.submit-btn {
  width: 100%;
  height: 40px;
  font-size: 16px;
}

.form-links {
  margin-top: 16px;
  display: flex;
  justify-content: space-between;
  color: #666;
  font-size: 14px;
}

.form-links span {
  cursor: pointer;
  transition: color 0.3s;
}

.form-links span:hover {
  color: var(--el-color-primary);
}

.verification-code {
  display: flex;
  gap: 12px;
}

.verification-code .el-input {
  flex: 1;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px var(--el-color-primary) inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--el-color-primary) inset;
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #1e3c72, #2a5298);
  border: none;
  transition: all 0.3s;
}

:deep(.el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(42, 82, 152, 0.3);
}
</style>
